package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.passroad.PassRoadEventInfo;
import com.yanqu.road.entity.config.passroad.PassRoadInfo;
import com.yanqu.road.entity.config.passroad.PassRoadTransportInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RoadDaoImpl extends TempDao {

	public Map<Integer, PassRoadInfo> getRoadInfoMap() {
		Map<Integer, PassRoadInfo> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_pass_road order by id";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				PassRoadInfo data = parseData(rs);
				dataMap.put(data.getId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}

	public PassRoadInfo getRoadInfoByPassId(int passId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_pass_road where id = " + passId;
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				return parseData(rs);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return null;
	}

	public PassRoadInfo parseData(ResultSet rs) throws SQLException {
		PassRoadInfo data = new PassRoadInfo();
		data.setId(rs.getInt("id"));
		data.setMoneyListStr(rs.getString("moneyList"));
		data.setStrengthListStr(rs.getString("strengthList"));
		data.setExperienceStr(rs.getString("experience"));
		data.setPrestigeStr(rs.getString("prestige"));
		data.setRewards(rs.getString("rewards"));
		data.setBaseInspireConsume(rs.getString("baseInspireConsume"));
		data.setEventsIdStr(rs.getString("eventsId"));
		return data;
	}

	public Map<Integer, PassRoadEventInfo> getRoadEventInfoMap() {
		Map<Integer, PassRoadEventInfo> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_pass_event order by id";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				PassRoadEventInfo data = new PassRoadEventInfo();
				data.setId(rs.getInt("id"));
				data.setType(rs.getInt("type"));
				data.setRewards(rs.getString("rewards"));
				data.setParLists(rs.getString("parLists"));

				dataMap.put(data.getId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}

	public Map<Integer, PassRoadTransportInfo> getRoadTransportInfoMap() {
		Map<Integer, PassRoadTransportInfo> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_pass_transport order by id";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				PassRoadTransportInfo data = new PassRoadTransportInfo();
				data.setId(rs.getInt("id"));
				data.setType(rs.getInt("type"));
				data.setEffectPar(rs.getString("effectPar"));

				dataMap.put(data.getId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}
}
